package com.cslg.ac.demo.controller.post;


import com.cslg.ac.demo.constant.*;
import com.cslg.ac.demo.entity.post.HostEvaluation;
import com.cslg.ac.demo.entity.post.Post;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.exception.ParamIllegalException;
import com.cslg.ac.demo.service.like.LikeService;
import com.cslg.ac.demo.service.message.MessageService;
import com.cslg.ac.demo.service.post.PostService;
import com.cslg.ac.demo.service.text.TextFilterService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import javax.xml.soap.SAAJResult;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:post.properties"},encoding="utf-8")
public class PostController {
    @Autowired
    PostService postService;
    @Autowired
    TextFilterService textFilterService;
    @Autowired
    LikeService likeService;
    @Autowired
    MessageService messageService;
    @Value("${commentLength}")
    int commentLength;
    @Value("${trendLength}")
    int trendLength;
    @Value("${trendType}")
    int trendType;
    @Value("${commentType}")
    int commentType;
    @Value("${evaluationType}")
    int evaluation;

    @RequestMapping(value = "/post/getByPost", method = RequestMethod.POST)
    public void getPostsByPost(@RequestParam int targetId, @RequestParam int offset,
                               @RequestParam int num, PrintWriter out){
        Integer[] ids;
        if(offset < 1 || num < 0){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        try {
            ids = postService.getPostsByPost(targetId,offset - 1,num);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids.length < 1){
            ids = null;
        }
        Map<String,Object> param = new HashMap<>(1);
        param.put("ids",ids);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/post/getByUser", method = RequestMethod.POST)
    public void getPostsByUser(@RequestParam int userId, @RequestParam int postType,
                               @RequestParam int offset, @RequestParam int num, PrintWriter out){
        Integer[] ids;
        if(offset < 1 || num < 0){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        try {
            ids = postService.getPostsByUser(userId,postType,offset - 1,num);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids.length < 1){
            ids = null;
        }
        Map<String,Object> param = new HashMap<>(1);
        param.put("ids",ids);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/post/get")
    public void getPost(@RequestParam int id,@RequestParam int postType, PrintWriter out){
        Post post;
        HostEvaluation hostEvaluation;
        Integer userId;
        try {
            post = postService.getPost(id);
            hostEvaluation = postService.getHostEvaluation(id);
            userId = postService.getUserByPost(id);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>(1);
        if(postType == evaluation && post != null){
            param.put("evaluation", hostEvaluation);
        }
        post.setCreateTime(post.getCreateTime().substring(0, 19));
        param.put("post",post);
        param.put("userId", userId);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/post/delete", method = RequestMethod.POST )
    public void deletePost(@RequestParam int id,HttpSession session,PrintWriter out){
        User user = (User)session.getAttribute("user");
        int code;
        try {
            code = postService.deletePost(user.getId(),id);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }


    @RequestMapping(value = "/post/add" ,method = RequestMethod.POST)
    public void addPost(Post post,@RequestParam int postType,
                        @RequestParam(required = false, defaultValue = "-1")int evaluationType, @RequestParam(required = false, defaultValue = "-1")int hostId,
                        @RequestParam(required = false, defaultValue = "-1")int anonymous,
                        HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        int contextLength;
        if(post.getTargetId() == 0){
            contextLength = trendLength;
        }else {
            contextLength = commentLength;
        }
            if(!post.addValid(contextLength, evaluationType, anonymous, postType)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        String text;
        int id;
        try {

            text = textFilterService.getFilterString(post.getContext(), TextType.POST_TYPE);
            if(text == null){
                ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
                return;
            }
            post.setContext(text);
            id = postService.addPost(post, user.getId(), postType, hostId, evaluationType,anonymous);
        } catch (ParamIllegalException e) {
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>(1);
        param.put("id",id);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
    }


}
